package com.quan.weixin.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mzlion.easyokhttp.HttpClient;
import com.quan.weixin.WeixinConstant;

/**
 * 获取 Access_Token 公众号的全局唯一接口调用凭据
 */
public class AccessTokenApi {
	
	private static Logger logger = LoggerFactory.getLogger(AccessTokenApi.class);
	
	private static int repeat = 3;

	public static AccessToken getAccessToken() {
		AccessToken accessToken = HttpClient.get(API.get(API.ACCESS_TOKEN_URL, WeixinConstant.APPID, WeixinConstant.APPSECRET)).execute().asBean(AccessToken.class) ;
		if(accessToken.getErrcode() > 0) {
			logger.error("微信全局唯一接口调用凭证Access_Token获取[失败]：{}", accessToken.getErrmsg());
			repeat -- ;
			if(repeat > 0)
				getAccessToken() ;
		} else {
			repeat = 3 ;
		}
		logger.info("微信全局唯一接口调用凭证Access_Token获取[成功]:{}", accessToken);
		return accessToken ;
	}
}
